

import checkBoxItem from '../from/checkBoxItem'
import optionSide from './optionSide'
import util from './util';
let verify = {};
verify.is = util.is;
// var rules = util.rules;

var checkList = function (options) {
    this.options = $.extend({}, {
        checkAll: true,
        showKey: 'label',
        getKey: 'value',
        setKey: 'value',
        getType: 'array',
        data: [],
        onChange: function () { },
    }, options);

    this.html = $('<div class="checkList"></div>');

    this.children = [];
    this.event = {
        change: this.options.onChange,
    };

    var _this = this;

    this.CheckAll = new checkBoxItem({
        showKey: 'name',
        data: {
            name: '全部'
        },
        'default': false,
        onChange: (value) => {
            for (let i = 0; i < this.children.length; i++) {
                const element = this.children[i];
                if (!element.html.hasClass('layui-disabled')) {
                    element.set(value);
                };
            };
            var _data = _this.get(false, true);
            _this.event.change(null, null, _data);
        },
    });
    this.CheckAll.html.addClass('allCheckItem');

    this.html.append(this.CheckAll.html);
    if (this.options.checkAll) {
        this.CheckAll.html.show();
    } else {
        this.CheckAll.html.hide();
    };
    this.setData(this.options.data);

};

checkList.prototype.onChange = function (event) {
    this.event.change = event;
};

checkList.prototype.checkAll = function () {
    var isAllCheck = true;
    for (let i = 0; i < this.children.length; i++) {
        const element = this.children[i];
        if (!element.value) {
            isAllCheck = false;
            break;
        };
    };
    this.CheckAll.set(isAllCheck);
};
checkList.prototype.clearChild = function () {
    for (let i = 0; i < this.children.length; i++) {
        const element = this.children[i];
        element.html.remove();
    };
    this.children = [];
};

checkList.prototype.setData = function (data) {
    var _this = this;

    var dataType = verify.is(data);
    if (dataType == 'string' && data.indexOf('OptionSide:') != -1) {
        data = optionSide.get(data.split(':')[1]);
    };

    this.clearChild();
    for (let i = 0; i < data.length; i++) {
        const item = data[i];
        var _showKey = '';
        if (typeof this.options.showKey == 'function') {
            _showKey = this.options.showKey(item);
        } else {
            _showKey = this.options.showKey;
        };
        const _item = new checkBoxItem({
            showKey: _showKey,
            data: item,
            'default': false,
            onChange: function (value) {
                _this.checkAll();
                var _data = _this.get(false, true);
                _this.event.change(_item, value, _data);
            },
        });
        this.html.append(_item.html);
        this.children.push(_item);
    };
};

checkList.prototype.readonly = function (values, key) {
    // arguments
    for (let i = 0; i < values.length; i++) {
        const element = values[i];
        this.readonlyItem(element, key);
    };
};
checkList.prototype.readonlyItem = function (value, key) {
    for (let i = 0; i < this.children.length; i++) {
        const element = this.children[i];
        // console.log(element);
        if (element.options.data[key] && element.options.data[key] == value) {
            element.readonly(true);
        }
    };
};
checkList.prototype.set = function (data, key) {
    var childrenValueString = {};
    for (let i = 0; i < this.children.length; i++) {
        const item = this.children[i];
        if (this.options.setKey == 'object') {
            childrenValueString[JSON.stringify(item.options.data)] = item;
        } else {
            childrenValueString[item.options.data[this.options.setKey]] = item;
        };
        item.set(false);
    };
    if (this.options.getType != 'array') {
        data = data.split(this.options.getType);
    };
    for (let i = 0; i < data.length; i++) {
        const dataItem = data[i];
        if (this.options.setKey == 'object') {
            try {
                childrenValueString[JSON.stringify(dataItem)].set(true);
            } catch (error) {
                console.warn('checkList => 未找到对应赋值选项 = >' + dataItem);
            };
        } else {
            try {
                childrenValueString[dataItem].set(true);
            } catch (error) {
                console.warn('checkList => 未找到对应赋值选项 = >' + dataItem);
            };
        };
    };
    this.checkAll();
};

checkList.prototype.get = function (callback, ignore) {
    var array = [];
    for (let i = 0; i < this.children.length; i++) {
        const element = this.children[i];
        if (element.value) {
            array.push(element.options.data);
        };
    };

    if (this.options.getKey != 'object') {
        var _array = [];
        for (let i = 0; i < array.length; i++) {
            const element = array[i];
            _array.push(element[this.options.getKey]);
        };
        array = _array;
        if (this.options.getType != 'array') {
            array = array.join(this.options.getType);
        };
    };

    if (ignore) {
        return array;
    } else {
        callback(null, array);
    };
};










export default checkList;